<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Beans.Responses.LoginResponse"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Beans.Requests.LoginRequest"%>
<%@page import="Beans.Responses.LoginResponse"%>
<%
    //checks if a response was returned to a previous login attempt
    String loginErrorMessage = "";
    if(request.getAttribute(ParameterNames.RESPONSE_LOGIN) != null)
    {
        //retrieves the response message
        LoginResponse loginResponse = (LoginResponse)request.getAttribute(ParameterNames.RESPONSE_LOGIN);
        loginErrorMessage = "<div id=\"loginError\">"
                            + loginResponse.getException().getExceptionMessage()
                            + "</div>";
    }
    else if(request.getParameter("login") != null)
    {
        //builds the login request
        LoginRequest loginRequest = new LoginRequest(request.getParameter("login.username"),
                                                     request.getParameter("login.password"),
                                                     session,
                                                     request.getRemoteAddr());

        //sens the login request and retrieves the result of the submission
        SendingRequestResult result = 
                UIFunctions.sendServiceRequest(request, response, loginRequest, ParameterNames.Service.LOGIN_MANAGEMENT);

        switch(result)
        {
            case OK:
                response.sendRedirect("index.jsp");
                break;

            case INVALID_REQUEST:
                loginErrorMessage = "<div id=\"loginError\">Malformed Username/Password</div>";
                break;
                
            default:
                loginErrorMessage = "<div id=\"loginError\">Request Failed (" + result + ")</div>";
                break;
        }
    }
    else
        ;
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <style type="text/css">
            body{ background-color: #cccccc; }

            #loginElement
            {
                display: table;
                margin-left: auto;
                margin-right: auto;
                background: #999999;
                border: 1px solid #c8e6bc;
            }

            #loginError
            {
                font: italic;
                color: #cc0000;
                border-bottom: 1px solid #c8e6bc;
                text-align: center;
            }
        </style>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <div id="loginElement">
            <%= loginErrorMessage %>
            <form action="login.jsp" method="POST">
                Username: <input type="text" name="login.username"><br>
                Password: <input type="password" name="login.password">
                <input type="submit" name="login" value="Send">
            </form>
        </div>
    </body>
</html>
